(ns samber.services.user.logout
  (:require
    [toucan.db :as db]
    [honeysql.core :as hsql]
    [samber.db.models :as models]
    [java-time :as time]
    [clojure.tools.logging :as log]
    [samber.config :refer [env]]
    [promesa.exec :as exec]
    [cuerdas.core :as str]
    [samber.middleware.exception :as exception]
    [buddy.hashers :as hashers]
    [buddy.sign.jwt :as jwt]))

(defn logout [token]
  (let [entity (db/select-one models/User (:id token))]
    ;; check exists
    (if (empty? entity)
      (throw (ex-info "check" {:type ::exception/check :msg "user does not exists!"})))

    (db/insert! models/JwtBlacklist {:jti               (:jti token)
                                     :exp               (:exp token)
                                     :user_id           (:id entity)
                                     :created_at        (hsql/raw "now()")
                                     :updated_at        (hsql/raw "now()")})

    {:code 0 :msg "success"}))
